package com.demo.zyt.modules.app.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.demo.zyt.common.dto.enrollment.StudentDTO;
import com.demo.zyt.common.dto.majorplan.MajorPlanDTO;
import com.demo.zyt.common.utils.PageUtils;
import com.demo.zyt.common.utils.Query;

import com.demo.zyt.modules.app.dao.MajorEnrollmentDao;
import com.demo.zyt.modules.app.entity.MajorEnrollmentEntity;
import com.demo.zyt.modules.app.service.MajorEnrollmentService;
import com.demo.zyt.modules.app.utils.ConvertNameUtils;


@Service("majorEnrollmentService")
public class MajorEnrollmentServiceImpl extends ServiceImpl<MajorEnrollmentDao, MajorEnrollmentEntity> implements MajorEnrollmentService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<MajorEnrollmentEntity> page = this.selectPage(
                new Query<MajorEnrollmentEntity>(params).getPage(),
                new EntityWrapper<MajorEnrollmentEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public PageUtils queryByCondition(Map<String, Object> params) {
		
		Page<MajorPlanDTO> page = new Query<MajorPlanDTO>(params).getPage();
		
		List<MajorPlanDTO> majorPlanDTOs = this.baseMapper.queryByCondition(page, params);
		
		majorPlanDTOs.forEach(item -> {
			item.setSchoolImage("http://www.gaokaozyt.cn/images/" + item.getCollegeId()+ "/headImage.jpg");
		});
		
        page.setRecords(majorPlanDTOs);
        
        return new PageUtils<MajorPlanDTO>(page);
	}

	@Override
	public List<MajorEnrollmentEntity> queryMajorByCollegeId(Map<String, Object> params) {
		
		return this.baseMapper.queryMajorByCollegeId(params);
		
	}

	@Override
	public PageUtils queryStudent(Map<String, Object> params) {
		
		Page<StudentDTO> page = new Query<StudentDTO>(params).getPage();
		
		List<StudentDTO> studentDTOs = this.baseMapper.queryStudent(page, params);
		
		if ( !studentDTOs.isEmpty() ) {
			studentDTOs.forEach(item -> {
				item.setStudentName(ConvertNameUtils.checkNameLength(item.getStudentName()));
				if ( !StringUtils.isEmpty(item.getStudentId()) && item.getStudentId().length() > 6 ) {
					item.setStudentId(ConvertNameUtils.left(item.getStudentId(), ConvertNameUtils.THREE) + "******" + ConvertNameUtils.left(item.getStudentId(), ConvertNameUtils.FOUR));
				}
			});
		}
		
        page.setRecords(studentDTOs);
        
        return new PageUtils<StudentDTO>(page);
	}

}
